<?php

namespace App\Http\Controllers\admin\Goods;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class GoodsMainController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
       
        // dd($request);
        $goods = DB::table('goodsmain')->where('id',$id)->get();

        return view('admin.goods.GoodsMainIndex',compact('goods'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //添加商品颜色分类
         $id = $request->id;
        return view('admin.goods.goodsmainadd',compact('id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //添加存储
        $data = $request->except('_token','_method');
        // dd($data);
        //执行插入数据
        $row = DB::table('goodsmain')->insert($data);
        if($row){
            return redirect('/admin/goods')->with('success','添加成功');
        }else{
            return back()->with('error','添加成功');

        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
         $goods = DB::table('goodsmain')->where('gid',$id)->get();
         // dd($id);
        return view('admin.goods.GoodsMainIndex',compact('goods','id'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($code)
    {
        //
        // dd($code);
        $data = DB::table('goodsmain')->where('code',$code)->first();
        // dd($data);
        return view('admin.goods.GoodsMainEdit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->except('_token','_method');
        $pic=$request->file();
        // dd($pic);
        //添加图片
         if($request->hasFile('pic')){
            
           
           
            foreach($pic as $k=>$v)
            {
                
                 foreach($v as $kk=> $vv)
                 {

                    //获取后缀名
                     $ext = $vv->getClientOriginalExtension();
                     // dd($ext);
                     //检测后缀名
                    $arr = array('jpg','png','jpeg');
                    if(!in_array($ext,$arr)){
                            echo '<script type="text/javascript">
                                alert("上传文件不合法");
                                location.href="http://la.cn/test/add";
                            </script>';
                        }
                    //随机文件名
                    $name = time()+rand(1,99);
                    $vv->move('./uploads/',$name.'.'.$ext);
                    // echo '<img src="public/uploads/',$name.'.'.$ext.'" alt="">';
               
                    $img= self::suofang('./uploads/'.$name.'.'.$ext,100,100);
                    // dd($img);
                     $data["pic{$kk}"] = $name.'.'.$ext;
                 }

            }
            // dd($v);
               
        }
        
        $data['pic']=" ";
        // dd($data);
        $code = $data['code'];
        // dd($code);
        //执行数据插入
        $res = DB::table('goodsmain')->where('code',$code)->update($data);
        if($res){
            return redirect('/admin/goods')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除商品颜色分类详情
        // dd($id);
        $res = DB::table('goodsmain')->where('id',$id)->delete();
        if($res){
            return redirect('/admin/goods')->with('success','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

    //缩放图片
           public static function suofang($spath, $sw = 100, $sh = 100, $pre = 's_')
          {
          //1.创建画布
          //计算比例  100 100  要缩放的图像的宽高  1920 1200  //用小的比例
          $srcinfo = getimagesize($spath);
          //根据图片类型 去创建图像资源 ...
          $src = imagecreatefromjpeg($spath); //strrchr  aaaa.jpg
          
          if($sw/$srcinfo[0] > $sh/$srcinfo[1]){
              $bili = $sh/$srcinfo[1];
          }else{
              $bili = $sw/$srcinfo[0];
          }
          //舍去法取整
          $sw = floor($srcinfo[0] * $bili);
          $sh = floor($srcinfo[1] * $bili);


          $dst = imagecreatetruecolor($sw,$sh);

          //3.开始绘画
          imagecopyresampled($dst, $src, 0, 0, 0, 0, $sw, $sh, $srcinfo[0], $srcinfo[1]);

          //5.将图像保存为文件
          //随机文件名  //获取文件后缀名  目录
          //$ext = strrchr($spath,'.');
          //$filename = time().rand(1000,9999).$ext;
          $filename = rtrim(dirname($spath),'/').'/'.$pre.basename($spath);
          
          imagejpeg($dst, $filename);

          //6.销毁资源
          imagedestroy($dst);
          imagedestroy($src);
          
          //返回原文件名加前缀
          return $pre.basename($spath);
      }



}
